/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.rpc.controller  
 */
package com.apache.rpc.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.action.PlatFromAction;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.manager.InterfaceRegisterManager;

/**
 * description:  接口注册action
 * @author Hou Dayu 创建时间：2014-10-31  
 * @Copyright(c)2014:
 */
@Controller
@RequestMapping("/protocol/")
public class ProtocolAction extends PlatFromAction<InterfaceRegister> {

	@Autowired
	private InterfaceRegisterManager interfaceRegisterManager;

	protected BaseManager<InterfaceRegister> getBaseManager() {
		return interfaceRegisterManager;
	}

	protected String getPrefix() {
		return "protocol/interface-";
	}

	@RequestMapping("cache.action")
	@ResponseBody
	public ResultMsg cache(HttpServletRequest request) {
		ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
		vo.setMethodKey("initCache");
		interfaceRegisterManager.execute(vo);
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		return rmsg;
	}

	@RequestMapping("list!data.action")
	@ResponseBody
	public Object data(HttpServletRequest request, String page, String rows) {

		ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
		InterfaceRegister register = new InterfaceRegister();
		if (Validator.isNotNull(request.getParameter("beanId"))) {
			register.setBeanId(request.getParameter("beanId"));
		}
		if (Validator.isNotNull(request.getParameter("beanName"))) {
			register.setBeanName(request.getParameter("beanName"));
		}
		if (Validator.isNotNull(request.getParameter("flag"))) {
			register.setFlag(request.getParameter("flag"));
		}
		vo.setParams("pageSize", Validator.getDefaultStr(rows, "20"));
		vo.setParams("pageIndex", Validator.getDefaultStr(page, "1"));
		vo.setObj(register);
		Page pageList = interfaceRegisterManager.getPageInfo(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("total", pageList.getCount());
		mapData.put("rows", pageList.getPageObjects());
		return mapData;
	}

	@RequestMapping(value = "/save.action", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg save(HttpServletRequest request, HttpServletResponse response, InterfaceRegister item) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");
		String isAdd = getDefaultStr(request.getParameter("isAdd"), "true");
		ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
		if (Validator.isNull(item.getRegisterId())) {
			item.setTimestampCreate(System.currentTimeMillis());
			item.setUserIdCreate("");
		} else {
			isAdd = "false";
		}
		vo.setObj(item);
		String id = "";
		boolean mark = false;
		if ("false".equals(isAdd) && item != null) {
			mark = interfaceRegisterManager.editInfo(vo);
		} else {
			id = interfaceRegisterManager.saveInfo(vo);
		}
		if (("false".equals(isAdd) && !mark) || ("true".equals(isAdd) && isNull(id))) {
			rmsg.setFlag("F");
			rmsg.setMsg("操作失败！");
		}
		return rmsg;
	}
}
